<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use Mail;
use DB;
use App\Http\Requests;
use App\Http\Requests\AdminLoginPostRequest;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class QuestionController extends Controller
{
    /*
        显示后台客服中心分区添加页面
    */ 

        public function getPartadd()
        {
            return view('admin.service.partadd');
        }
    /*
        后台客服中心分区添加执行
    */
        public function postDopartadd(Request $request)
        {
            // dd($request);
          $data = $request->only(['partname']);
           // 执行数据插入操作
	      $res = DB::table('service_part')->insert($data);
	      // 判断
	      if($res){
	        return redirect('admin/service/partadd')->with('success','分区添加成功!!');
	      }else{
	        return back()->with('error','分区添加失败!!');
	      }
        
    }
        

    /*
        显示后台客服中心分区列表页面
    */ 
       
     	public function getPartlist(Request $request)
    	{

	      $num = $request->input('num',10);
	      // 判断是否有搜索词
	      if($request->input('keywords')){
	          $partname = DB::table('service_part')->where('partname','like','%'.$request->input('keywords').'%')->paginate($num);
	      }else{
	          $partname = DB::table('service_part')->paginate($num);
	      }
	      $data = $partname->all();
	      // // 查询分类名
	      // foreach($data as $k=>$v){
	      //     $names = DB::table('dota_cate')->where('id',$v->pid)->value('name');  
	      //     // 将pid改为分类名
	      //     $v->pid = $names;   
	      // }
	      // 解析模板
	      return view('admin.service.partlist',['partname'=>$partname,'data'=>$data,'request'=>($request->all()),'num'=>$num]);
	    }

	/*
      分块添加页
    */
	    public function getCateadd(Request $request)
	    {
	      // 查询所有分区
	      $partname = DB::table('service_part')->get();
	      // dd($partname);
	  
	      // 解析模板
	      return view('admin.service.cateadd',['partname'=>$partname]);
	    }
        
     /*
        执行分块添加
    */
	    public function postDocateadd(Request $request)
	    {
	        
	        // 进行表单验证用户填写是否正确
	    	// dd($request->all());

	          $this->validate($request, [
	            'catename' => 'required',
	            
	          ],[
	            'catename.required' => '请填写分块名称!!',
	            
	          ]);
	          // dd($request->all());

	          if ($request->pid == 0) {
	          	return back()->with('error','请选择分区!!');
	          }
	        $data = $request->only(['catename','pid']);
	        
	        // 根据pid查询path
	        $res = DB::table('service_part')->where('id',$data['pid'])->first();
	        $data['partname'] = $res->partname;
	        // 执行数据插入操作
	        
	        $res = DB::table('service_cate')->insert($data);
	        if($res){
	            return redirect('/admin/service/cateadd')->with('success','分块添加成功!!');
	        }else{
	            return back()->with('error','分块添加失败!!');
	        }
	    }

	/*
        显示后台客服中心分块列表页面
    */ 
       
     	public function getCatelist(Request $request)
    	{
 		 $num = $request->input('num',10);
 		 // dd(DB::table('service_cate')->paginate($num)->all());
	      
	      // 判断是否有搜索词
 		 // dd($request->input('keywords'));
	      if($request->input('keywords')){
	          $catename = DB::table('service_cate')->where('catename','like','%'.$request->input('keywords').'%')->paginate($num);
	      }else{
	      		
	          $catename = DB::table('service_cate')->paginate($num);
	      	
	      }
	     
	      $data = $catename->all();
	      // // 查询分类名
	      // foreach($data as $k=>$v){
	      //     $names = DB::table('dota_cate')->where('id',$v->pid)->value('name');  
	      //     // 将pid改为分类名
	      //     $v->pid = $names;   
	      // }
	      // 解析模板
	      return view('admin.service.catelist',['catename'=>$catename,'data'=>$data,'request'=>($request->all()),'num'=>$num]);
	    }

	/*
      	问题添加页
    */
	    public function getQuestionadd(Request $request)
	    {
	      // 查询所有分区分块
	      $cates = DB::table('service_cate')->get();
	      $parts = DB::table('service_part')->get();
	      // dd($res);
	      // dd($partname);
	  
	      // 解析模板
	      return view('admin.service.questionadd',['cates'=>$cates,'parts'=>$parts]);
	    }

	/*
        执行问题添加
    */
	    public function postDoqsnadd(Request $request)
	    {
	        
	        // 进行表单验证用户填写是否正确
	    	 // dd($request->all());

	          $this->validate($request, [
	            'qtitle' => 'required',
	            'qcontent' => 'required',
	            
	          ],[
	            'qtitle.required' => '请填写问题标题!!',
	            'qcontent.required' => '请填写回答内容!!',
	            
	          ]);
	          // dd($request->all());

	          if (($request->pid == 0) || ($request->cid == 0)) {
	          	return back()->with('error','请选择分区!!');
	          }
	        $data = $request->only(['cid','pid','qtitle','qcontent']);
	        
	        // 根据pid/cid查询part/catename
	        $partname = DB::table('service_part')->where('id',$data['pid'])->first();
	        $data['partname'] = $partname->partname;
	        $catename = DB::table('service_cate')->where('id',$data['cid'])->first();
	        $data['catename'] = $catename->catename;

	        // dd($data);
	        // 执行数据插入操作
	        $res = DB::table('service_qsn')->insert($data);
	        if($res){
	            return redirect('/admin/service/questionadd')->with('success','问题添加成功!!');
	        }else{
	            return back()->with('error','问题添加失败!!');
	        }
	    }

	/*
        显示后台客服中心问题列表页面
    */ 
       
     	public function getQuestionlist(Request $request)
    	{
 		 $num = $request->input('num',10);
 		 // dd(DB::table('service_cate')->paginate($num)->all());
	      
	      // 判断是否有搜索词
 		 // dd($request->input('keywords'));
	      if($request->input('keywords')){
	          $qsn = DB::table('service_qsn')->where('qtitle','like','%'.$request->input('keywords').'%')->paginate($num);
	      }else{
	      		
	          $qsn = DB::table('service_qsn')->paginate($num);
	      	
	      }
	     
	      $data = $qsn->all();
	      // // 查询分类名
	      // foreach($data as $k=>$v){
	      //     $names = DB::table('dota_cate')->where('id',$v->pid)->value('name');  
	      //     // 将pid改为分类名
	      //     $v->pid = $names;   
	      // }
	      // 解析模板
	      return view('admin.service.questionlist',['qsn'=>$qsn,'data'=>$data,'request'=>($request->all()),'num'=>$num]);
	    }    

	  
}
